<?php
// +----------------------------------------------------------------------
// author laobai
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
namespace app\paiban\controller;

use think\facade\Db;
use think\facade\Session;

class Admin extends Common
{
    public function __construct(){
        parent::__construct();
        
        $this->IsLogin();
    }
    
    //sql  user:panban,pass:kbkpHXMXwYYszM2X
    public function index()
    {
        $map_user=array();
        if($this->user['is_admin']==0)
        {
            $map_user[]=['bumen_id','=',$this->user['bumen_id']];
            $map_user[]=['is_admin','=',0];
        }
        $bumen=Db::name('bumen')->where(['id'=>$this->user['bumen_id']])->find();
        if(!$bumen)
        {
            $bumen['name']='';
        }
        $months=array();
        $datetime=date('Y-m-d');
        $days = date('t');
        for($i=1;$i<=$days;$i++)
        {
            array_push($months,$i);
        }
        $this->assign('bumen',$bumen);
        $this->assign('months',$months);
        $banlei_shangwu=array();
        $banlei_xiawu=array();
        $m=date('n');
        $y=date('Y');
        $day=date('d');
        $user_arr=Db::name('users')->where($map_user)->select();//paginate(10);
        $userban=array();
        foreach ($user_arr as $u)
        {
            for($i=1;$i<=$days;$i++)
            {
                //上午
                $data = Db::name('ban')->where(['user_id'=>$u['id'],'ban_month'=>$m,'ban_day'=>$i,'banci'=>1])->order('banci asc')->find();
                if($data)
                {
                    $banlei=Db::name('banlei')->where(['id'=>$data['banlei']])->find();
                    $banlei_shangwu[]=['banlei'=>$banlei['name'],'mark'=>str_replace(array("\r\n", "\r", "\n"), "",  $data['mark'])];
                }
                else{$banlei_shangwu[]=['banlei'=>'-','mark'=>''];}
               
            }
            
            for($i=1;$i<=$days;$i++)
            {
                //下午
                $data = Db::name('ban')->where(['user_id'=>$u['id'],'ban_month'=>$m,'ban_day'=>$i,'banci'=>2])->order('banci asc')->find();
                if($data)
                {
                    $banlei=Db::name('banlei')->where(['id'=>$data['banlei']])->find();
                    $banlei_xiawu[]=['banlei'=>$banlei['name'],'mark'=>str_replace(array("\r\n", "\r", "\n"), "",  $data['mark'])];
                }
                else{$banlei_xiawu[]=['banlei'=>'-','mark'=>''];}
               
            }
            $u['shangwu']=$banlei_shangwu;
            $u['xiawu']=$banlei_xiawu;
            $u['jiaban']=Db::name('jiaban')->where(['user_id'=>$u['id'],'year'=>$y,'month'=>$m])->value('days',0);
            
            $userban[]=$u;
            $banlei_shangwu=array();
            $banlei_xiawu=array();
        }
        $page='';//$user->render();
        $this->assign('page',$page);
        $datetime=['date'=>$datetime,'year'=>$y,'month'=>$m,'day'=>$day];
        $this->assign('datetime',$datetime);
        $this->assign('userban',$userban);
        $this->assign('shangwu',$banlei_shangwu);
        $this->assign('xiawu',$banlei_xiawu);
        $this->assign('admin',$this->user);
        return $this->fetch();
        
    }
    //加班保存
    public function jiabansave(){
        $p=input();
        $datetime=strtotime($p['datetime']);
        $year=date('Y',$datetime);
        $month=date('n',$datetime);
        $day=date('d',$datetime);
        $data=['user_id'=>$p['user_id'],
            'date_time'=>$datetime,
            'days'=>$p['days'],
            'year'=>$year,
            'month'=>$month,
            'day'=>$day,
        ];
        $map=array();
        $map[]=['user_id','=',$p['user_id']];
        $map[]=['date_time','=',$datetime];
        if(Db::name('jiaban')->where($map)->count()==0)
        {
            $res = Db::name('jiaban')->insert($data);
        }
        else
        {
            $res = Db::name('jiaban')->where($map)->update(['days'=>$p['days']]);
        }
        if($res)
        {
            return $this->success('succ');
        }
        return $this->error('操作失败');
    }
    //人员
    public function userEdit()
    {
        $p=input();
        $data=Db::name('users')->where('id','=',$p['id'])->update(['name'=>$p['name']]);
        return $this->success('ok');
    }
     public function userRemove()
    {
        $p=input();
        $data=Db::name('users')->where('id','=',$p['id'])->where('id','>',0)->delete();
        return $this->success('ok');
    }
    public function userSave()
    {
        $p=input();
        if(empty($p['id']))
        {
            $res=Db::name('users')->insert($p);
        }
        else
        {
            $user_id=$p['id'];
            unset($p['id']);
            $res=Db::name('users')->where(['id'=>$user_id])->update($p);
        }
        return $this->success('ok');
    }
    public function users()
    {
        $p=input();
        $user_arr=array();
        $map_user=array();
        if($this->user['is_admin']==0)
        {
            $map_user[]=['bumen_id','=',$this->user['bumen_id']];
            $map_user[]=['is_admin','=',0];
        }
        if(isset($p['id']))
        {
            $user_arr=Db::name('users')->where($map_user)->where(['id'=>$p['id']])->find();
        }
       
        $this->assign('user_arr',$user_arr);
        $list=Db::name('users')->where($map_user)->order('id desc')->paginate(20);
        $data=array();
        foreach ($list as $v)
        {
            $v['bumen']['name']='';
            $bumen=Db::name('bumen')->where(['id'=>$v['bumen_id']])->find();
            if($bumen)
            {
                $v['bumen']=$bumen;
            }
            $data[]=$v;
        }
        $page = $list->render();
        $this->assign('list',$data);
        $this->assign('page',$page);
        $bumen=Db::name('bumen')->order('id desc')->select();
        $this->assign('bumen',$bumen);
        
        $this->assign('admin',$this->user);
        return $this->fetch();
    }
    //部门
    public function bumenEdit()
    {
        $p=input();
        $data=Db::name('bumen')->where('id','=',$p['id'])->update(['name'=>$p['name']]);
        return $this->success('ok');
    }
     public function bumenRemove()
    {
        $p=input();
        $data=Db::name('bumen')->where('id','=',$p['id'])->delete();
        return $this->success('ok');
    }
    public function bumenSave()
    {
        $p=input();
        $data=Db::name('bumen')->insert(['name'=>$p['name']]);
        return $this->success('ok');
    }
    public function bumen()
    {
        $list=Db::name('bumen')->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('admin',$this->user);
        return $this->fetch();
    }
    //班类
    public function banleiRemove()
    {
        $p=input();
        $data=Db::name('banlei')->where('id','=',$p['id'])->delete();
        return $this->success('ok');
    }
    public function banleiSave()
    {
        $p=input();
        $data=Db::name('banlei')->insert(['name'=>$p['name']]);
        return $this->success('ok');
    }
    public function banlei()
    {
        $list=Db::name('banlei')->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('admin',$this->user);
        return $this->fetch();
    }
    //添加排班
    public function banlist()
    {
        $map_user=array();
        $map_user[]=['id','>',3];
        if($this->user['is_admin']==0)
        {
            $map_user[]=['bumen_id','=',$this->user['bumen_id']];
            $map_user[]=['is_admin','=',0];
        }
        $bumen=Db::name('bumen')->where(['id'=>$this->user['bumen_id']])->find();
        if(!$bumen)
        {
            $bumen['name']='';
        }
        $months=array();
        $datetime=date('Y-m-d');
        $days = date('t');
        //for($i=1;$i<=$days;$i++)
        for($i=1;$i<32;$i++)
        {
            array_push($months,$i);
        }
        
        
        $m=date('n');
        $y=date('Y');
        $day=date('d');
        $users_arr=Db::name('users')->where($map_user)->select();//paginate(10);
        $userban=array();
        foreach ($users_arr as $us)
        {
            $banlei_shangwu=Db::name('bans')->where(['user_id'=>$us['id'],'ban_month'=>$m,'ban_year'=>$y,'banci'=>2])->find();//$this->getBanShangwu

            $banlei_xiawu=Db::name('bans')->where(['user_id'=>$us['id'],'ban_month'=>$m,'ban_year'=>$y,'banci'=>1])->find();
            $xiawu=array();
            
            $us['shangwu']=$banlei_shangwu;
            $us['xiawu']=$banlei_xiawu;
            $us['jiaban']=Db::name('jiaban')->where(['user_id'=>$us['id'],'year'=>$y,'month'=>$m])->value('days',0);
            $userban[]=$us;
        }
        $page='';//$user->render();
        $this->assign('bumen',$bumen);
        $this->assign('months',$months);
        $this->assign('page',$page);
        $datetime=['date'=>$datetime,'year'=>$y,'month'=>$m,'day'=>$day];
        $this->assign('datetime',$datetime);
        $this->assign('userban',$userban);
        $this->assign('shangwu',$banlei_shangwu);
        $this->assign('xiawu',$banlei_xiawu);
        $this->assign('admin',$this->user);
        return $this->fetch();
        
    }
    function getBanShangwu($user_id,$year,$month)
    {
        $tmp_d = Db::name('bans')->where(['user_id'=>$user_id,'ban_month'=>$month,'ban_year'=>$year,'banci'=>2])->select();
       
        $shangwu=array();
        foreach ($tmp_d as $k=>$v)
        {
            if(substr($k,0,1)=='d')
            {
                if($v==0)
                {
                    $v='-';
                }
            }
            $shangwu[$k]=$v;
        }
        return $shangwu;
    }
    public function bans()
    {
        $admin=Session::get('user');
        if(empty($admin))
        {
            $this->redirect('/paiban/index/login');
            die;
        }
        if($admin['is_manager']==0 && $admin['is_admin']==0)
        {
            $this->redirect('/paiban/index/login');
            die;
        }
        $this->assign('admin',$admin);
        $map=array();
        $map[]=['bumen_id','=',$admin['bumen_id']];
        $user=Db::name('users')->where($map)->order('id desc')->select();
        $this->assign('user',$user);
        $banlei=Db::name('banlei')->order('id desc')->select();
        $this->assign('banlei',$banlei);
        return $this->fetch();
    }
    public function bansave()
    {
        $p=input();
        if($p['ban_date']=='')
        {
            $p['ban_date']=date('Y-m-d');
        }
        $ban_month=date('m',strtotime($p['ban_date']));
        $ban_day = intval(date('d',strtotime($p['ban_date'])));
        $ban_year=date('Y',strtotime($p['ban_date']));
        $p['ban_year']=$ban_year;
        $p['ban_month']=$ban_month;
        $p['ban_day'] =$ban_day;
        $banci=$p['banci'];
        if($banci==4)
        {
            $p['banci']=1;
            $this->addBan($p);
            $p['banci']=2;
            $res = $this->addBan($p);
        }
        else
        {
            $res = $this->addBan($p);
        }
        if($res)
        {
            return $this->success('succ');
        }
        return $this->success('fail');
    }
    private function addBan($p)
    {
        $res=Db::name('bans')->where(['user_id'=>$p['user_id'],'ban_year'=>$p['ban_year'],'ban_month'=>$p['ban_month'],'banci'=>$p['banci']])->find();
        if(!$res)
        {
            $data['add_time']=time();
            $data['user_id']=$p['user_id'];
            $data['ban_year']=$p['ban_year'];
            $data['ban_month']=$p['ban_month'];
            $data['banci']=$p['banci'];
            $data['d'.$p['ban_day']]=Db::name('banlei')->where(['id'=>$p['banlei']])->value('name','');
            $res = Db::name('bans')->insert($data);
        }
        else {
            $banlei=Db::name('banlei')->where(['id'=>$p['banlei']])->value('name','');
            $res = Db::name('bans')->where(['id'=>$res['id']])->update(['d'.$p['ban_day']=>$banlei]);
        }
        return $res;
    }
}